package com.wzy.orderSystem.service.impl;

import com.wzy.orderSystem.dao.FoodDao;
import com.wzy.orderSystem.pojo.food_detail;
import com.wzy.orderSystem.service.opFoodDetail;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class opFoodDetailImpl implements opFoodDetail {
    @Override
    public List<food_detail> selectById(int id) {
        ArrayList<food_detail> food_details = new ArrayList<>();
        ResultSet resultSet = null;
        try{
            String sql="SELECT * FROM `food_detail` WHERE id=?";
            Object[] obj = {id};
            resultSet = FoodDao.selectByArgs(sql, obj);
            while(resultSet.next()){
                int  foodId = resultSet.getInt(1);
                String foodName = resultSet.getString(2);
                int foodPrice = resultSet.getInt(3);
                String foodInfo = resultSet.getString(4);
                String foodClassify = resultSet.getString(5);
                int favorite = resultSet.getInt(6);
                String foodPic = resultSet.getString(7);
                food_detail food_detail = new food_detail();
                food_detail.setId(foodId);
                food_detail.setFoodName(foodName);
                food_detail.setFoodPrice(foodPrice);
                food_detail.setFoodInfo(foodInfo);
                food_detail.setFoodClassify(foodClassify);
                food_detail.setFavorite(favorite);
                food_detail.setFoodPic(foodPic);

                food_details.add(food_detail);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return food_details;
    }

    @Override
    public int updateByName(String foodName, int foodPrice) {
        int rows = 0;
        String sql = "UPDATE `food_detail` SET food_price=? WHERE food_name=?";
        Object[] obj = {foodPrice,foodName};
        rows= FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int deleteByName(String foodName) {
        int rows = 0;
        String sql = "DELETE FROM `food_detail` WHERE food_name=?";
        Object[] obj = {foodName};
        rows= FoodDao.updateByArgs(sql, obj);
        return rows;
    }

    @Override
    public int insert(int id, String foodName, int foodPrice, String foodInfo, String classify, int favorite, String foodPic) {
        int rows = 0;
        String sql = "INSERT INTO `orderfood`.`food_detail` (`id`, `food_name`, `food_price`, `food_info`, `food_classify`, `favorite`, `food_pic`) VALUES (?, ?, ?, ?, ?, ?, ?)";
        Object[] obj = {id,foodName,foodPrice,foodInfo,classify,favorite,foodPic};
        rows= FoodDao.updateByArgs(sql, obj);
        return rows;
    }
}
